package exception;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

//字符流的练习：PWDemo2简要记事本按行写+BRDemo按行读
//异常的练习：
//try...catch：TryCatchDemo
//finally：FinallyDemo+FinallyDemo2+AutoCloseableDemo
//throw：Student+ThrowDemo
public class Work {
    public static void main(String[] args) throws IOException {
        //字符流的练习
        /*
        FileOutputStream fos = new FileOutputStream("pw.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        Scanner scan = new Scanner(System.in);
        System.out.println("请开始输入内容，单独输入exit时退出");
        while (true){
            String line = scan.nextLine();
            if ("exit".equals(line)){
                break;
            }
            pw.println(line);
        }
        pw.close();
         */
        /*
        FileOutputStream fos = new FileOutputStream("pw.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        Scanner scan = new Scanner(System.in);
        System.out.println("请开始输入内容，单独输入exit时退出");
        while (true){
            String line = scan.nextLine();
            if ("exit".equals(line)){
                break;
            }
            pw.println(line);
        }
        pw.close();

         */
        /*
        FileInputStream fis = new FileInputStream("pw.txt");
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine())!=null){
            System.out.println(line);
        }
        br.close();

         */
        /*
        FileInputStream fis = new FileInputStream("pw.txt");
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine())!=null){
            System.out.println(line);
        }
        br.close();

         */
        //异常的练习：
        //try...catch
        /*
        System.out.println("程序开始了...");
        try {
            String line1 = null;
            System.out.println(line1.length());

            String line = "abc";
            System.out.println(Integer.parseInt(line));


            String line2 = "";
            System.out.println(line2.charAt(0));
        }catch (NullPointerException | StringIndexOutOfBoundsException e){
            System.out.println("出现空指针或字符串下标越界异常并解决了");
        }catch (Exception e){
            System.out.println("反正就是出了个错并解决了");
        }
        System.out.println("程序结束了");

         */
        /*
        System.out.println("程序开始了...");
        try {
            String line1 = null;
            System.out.println(line1.length());

            String line = "abc";
            System.out.println(Integer.parseInt(line));


            String line2 = "";
            System.out.println(line2.charAt(0));
        }catch (NullPointerException | StringIndexOutOfBoundsException e){
            System.out.println("出现空指针或字符串下标越界异常并解决了");
        }catch (Exception e){
            System.out.println("反正就是出了个错并解决了");
        }
        System.out.println("程序结束了");

         */
        //finally
        /*
        System.out.println("程序开始了...");
        try {
            String line = "";
            System.out.println(line.length());
            return;
        }catch (Exception e){
            System.out.println("出错并解决了");
        }finally {
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了...");

         */
        /*
        System.out.println("程序开始了...");
        try {
            String line = "";
            System.out.println(line.length());
            return;
        }catch (Exception e){
            System.out.println("出错并解决了");
        }finally {
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了...");

         */
        /*
        System.out.println("程序开始了...");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("fos.dat");
            fos.write(1);
        } catch (IOException e) {
            System.out.println("出现IO异常并解决了");
        }finally {
            try {
                fos.close();
            }catch (Exception e){
                System.out.println("关闭流时出现异常并解决了");
            }
        }
        System.out.println("程序结束了");

         */
        /*
        System.out.println("程序开始了...");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("fos.dat");
            fos.write(1);
        } catch (IOException e) {
            System.out.println("出现IO异常并解决了");
        }finally {
            try {
                fos.close();
            }catch (Exception e){
                System.out.println("关闭流时出现异常并解决了");
            }
        }
        System.out.println("程序结束了");
         */
        /*
        System.out.println("程序开始了");
        try(
                FileOutputStream fos = new FileOutputStream("fos.dat");
        ) {
            fos.write(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("程序结束了");
         */
        /*
        System.out.println("程序开始了");
        try(
            FileOutputStream fos = new FileOutputStream("fos.dat");
        ){
            fos.write(1);
        }catch (IOException e ){
            e.printStackTrace();
        }
        System.out.println("程序结束了");

         */
        //throw
        /*
        Student1 zs = new Student1();
        try {
            zs.setAge(1000);
            System.out.println("此人的年龄为:"+zs.getAge()+"岁");
        }catch (RuntimeException e ){
            System.out.println("年龄不合法");
        }

         */
        /*
        Student1 zs = new Student1();
        try {
            zs.setAge(1000);
            System.out.println("此人的年龄为："+zs.getAge()+"岁");
        }catch (RuntimeException e ){
            System.out.println("年龄不合法");
        }

         */
    }
}
